home *** CD-ROM | disk | FTP | other *** search
/ PC Open 100 / PC Open 100 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / bayes.pl < prev    next >
Encoding:
Perl Script  |  2004-03-15  |  2.9 KB  |  120 lines

  1. #!/usr/bin/perl
  2. # ---------------------------------------------------------------------------------------------
  3. #
  4. # bayes.pl --- Classify a mail message manually
  5. #
  6. # Copyright (c) 2001-2003 John Graham-Cumming
  7. #
  8. #   This file is part of POPFile
  9. #
  10. #   POPFile is free software; you can redistribute it and/or modify
  11. #   it under the terms of the GNU General Public License as published by
  12. #   the Free Software Foundation; either version 2 of the License, or
  13. #   (at your option) any later version.
  14. #
  15. #   POPFile is distributed in the hope that it will be useful,
  16. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. #   GNU General Public License for more details.
  19. #
  20. #   You should have received a copy of the GNU General Public License
  21. #   along with POPFile; if not, write to the Free Software
  22. #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. #
  24. # ---------------------------------------------------------------------------------------------
  25.  
  26. use strict;
  27. use Classifier::Bayes;
  28. use Classifier::WordMangle;
  29. use POPFile::Configuration;
  30. use POPFile::MQ;
  31. use POPFile::Logger;
  32.  
  33. # main
  34.  
  35. my $code = 0;
  36.  
  37. if ( $#ARGV >= 0 ) {
  38.     my $c = new POPFile::Configuration;
  39.     my $mq = new POPFile::MQ;
  40.     my $l = new POPFile::Logger;
  41.     my $b = new Classifier::Bayes;
  42.     my $w = new Classifier::WordMangle;
  43.  
  44.     $c->configuration( $c );
  45.     $c->mq( $mq );
  46.     $c->logger( $l );
  47.  
  48.     $c->initialize();
  49.  
  50.     $l->configuration( $c );
  51.     $l->mq( $mq );
  52.     $l->logger( $l );
  53.  
  54.     $l->initialize();
  55.  
  56.     $w->configuration( $c );
  57.     $w->mq( $mq );
  58.     $w->logger( $l );
  59.  
  60.     $w->start();
  61.  
  62.     $mq->configuration( $c );
  63.     $mq->mq( $mq );
  64.     $mq->logger( $l );
  65.  
  66.     $b->configuration( $c );
  67.     $b->mq( $mq );
  68.     $b->logger( $l );
  69.  
  70.     $b->{parser__}->mangle( $w );
  71.     $b->initialize();
  72.  
  73.     $c->load_configuration();
  74.  
  75.     $b->start();
  76.  
  77.     my $session = $b->get_session_key( 'admin', '' );
  78.  
  79.     my @files;
  80.  
  81.     if ($^O =~ /linux/) {
  82.         @files = @ARGV[0 .. $#ARGV];
  83.     } else {
  84.         @files = map { glob } @ARGV[0 .. $#ARGV];
  85.     }
  86.  
  87.     foreach my $file (@files) {
  88.         if ( !(-e $file) ) {
  89.             print STDERR "Error: File `$file' does not exist, classification aborted.\n";
  90.             $code = 1;
  91.             last;
  92.         }
  93.     }
  94.  
  95.     if ( $code == 0 ) {
  96.         foreach my $file (@files) {
  97.             print "`$file' is `" . $b->classify( $session, $file ) . "'\n";
  98.         }
  99.  
  100.         foreach my $word (sort keys %{$b->{parser__}->{words__}}) {
  101.             print "$word $b->{parser__}->{words__}{$word}\n";
  102.         }
  103.     }
  104.  
  105.     $b->release_session_key( $session );
  106.     $b->stop();
  107.     $l->stop();
  108.     $mq->stop();
  109.     $c->stop();
  110. }
  111. else
  112. {
  113.     print "bayes.pl - output the classification of a message\n\n";
  114.     print "Usage: bayes.pl <messages>\n";
  115.     print "       <messages>         Filename of message(s) to classify\n";
  116.     $code = 1;
  117. }
  118.  
  119. exit $code;
  120.